﻿
@{
    ViewBag.Title = "ConfigPrice";
    Layout = "~/Views/Shared/layoutCommon.cshtml";
}

@section style{
    <style>
        .col-sm-2 {
            width: 20%;
            float: left;
            text-align: right;
            margin-top: 5px;
        }

        .col-sm-3 {
            width: 30%;
            float: left;
        }
        .col-sm-1 {
            margin-top: 5px;
        }
    </style>
}
<form id="form1">
    <section class="content-header">
        <h1>
            @ViewBag.CityName 配置业务
            <small></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
            <li><a href="@Url.Content("~/config/CityOpenManage")?provinceid=@ViewBag.ProvinceId">省市业务开通</a></li>
            <li><a href="@Url.Content("~/config/FormBusiness")?cityid=@ViewBag.CityId&provinceid=@ViewBag.ProvinceId&cityname=@ViewBag.CityName">配置业务</a></li>
            <li class="active">配置表单</li>
        </ol>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header">
                        <div class="bs-example">
                            <ul class="nav nav-tabs">
                                <li role="presentation"><a href="@Url.Content("~/Config/ConfigForm?provinceid="+@ViewBag.ProvinceId+"&cityname="+@ViewBag.CityName+"&cgfid="+@ViewBag.CgfId+"")">配置表单</a></li>
                                <li role="presentation" class="active"><a href="#">配置价格</a></li>
                                <li role="presentation"><a href="@Url.Content("~/Config/ConfigOrderExpire?provinceid="+@ViewBag.ProvinceId+"&cityname="+@ViewBag.CityName+"&cgfid="+@ViewBag.CgfId+"")">网办订单过期</a></li>
                                <li role="presentation"><a href="@Url.Content("~/Config/ConfigGuidance?provinceid="+@ViewBag.ProvinceId+"&cityname="+@ViewBag.CityName+"&cgfid="+@ViewBag.CgfId+"")">配置说明</a></li>
                                <li role="presentation"><a href="@Url.Content("~/Config/ConfigExpressTemplate?provinceid="+@ViewBag.ProvinceId+"&cityname="+@ViewBag.CityName+"&cgfid="+@ViewBag.CgfId+"")">配置快递</a></li>
                                <li role="presentation"><a href="@Url.Content("~/Config/ConfigView?provinceid="+@ViewBag.ProvinceId+"&cityname="+@ViewBag.CityName+"&cgfid="+@ViewBag.CgfId+"")">配置预览</a></li>
                            </ul>
                        </div>

                        <h3>
                            <i class="fa fa-fw fa-arrow-right"></i>@ViewBag.BizTypeName
                        </h3>
                    </div>
                    <!-- /.box-header -->
                    <div class="form-horizontal">
                        <div class="box-body">
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="name">工本费:</label>
                                <div class="col-sm-3">
                                    <input type="text" class="form-control" value="" style="width:300px;" name="txtFee" maxlength="50" id="txtFee" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="name">
                                    <button type="button" class="btn btn-success btn-sm" id="btnAdd">添加自定义费用</button>
                                </label>
                                <div class="col-sm-3">
                                    <input type="hidden" id="priceIds" />
                                </div>
                            </div>
                            <div id="CustomBody">

                            </div>
                        </div>
                    </div>
                    <div class="box-footer text-center">
                        <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                        <button class="btn" type="button" id="btnCancel">取 消 </button>
                    </div>
                    <!-- /.box-body -->
                    <!-- /.box -->
                </div>
            </div>
        </div>
    </section>
</form>
@section scriptsBottom{
    <script type="text/javascript">
        $(document).ready(function () {
            initFormValidate();
            $("#btnSave").click(function () {
                save();
            });
            $("#btnCancel").click(function () {
                cancel();
            });

            $("#btnAdd").click(function () {
                var len = $("#CustomBody").find(".simple").length;
                if (len > 1)
                {
                    BMCW.alert("最多添加两条自定义费用");
                    return;
                }
                var row = divFeeRow("","","");
                $("#CustomBody").append(row);
                addValidate();

            });

            initFee();
        });

        //自定义费用
        function divFeeRow(feeName,fee,id)
        {
            var div = "<div class=\"form-group\">"
                             + "          <label class=\"col-sm-2 control-label\" for=\"name\">自定义费用名称:</label>"
                             + "          <div class=\"col-sm-3\">"
                             + "              <input type=\"text\" class=\"form-control\" style=\"width:300px;\" value=\""+feeName+"\" name=\"txtFeeName\" maxlength=\"20\" id=\"txtFeeName\"  />"
                             + "         </div><div class=\"col-sm-1\">"
                             + "              <button type=\"button\" class=\"btn btn-danger btn-xs\" onclick=\"deleteRow($(this));\">删除</button><input type=\"hidden\" name=\"hidFeeId\" id=\"hidFeeId\" value=\""+id+"\" />"
                             + "          </div>"
                             + "     </div>"
                             + "     <div class=\"form-group\">"
                             + "         <label class=\"col-sm-2 control-label\" for=\"name\">自定义费用金额:</label>"
                             + "        <div class=\"col-sm-3\">"
                             + "           <input type=\"text\" class=\"form-control\" style=\"width:300px;\" value=\""+fee+"\" name=\"txtFee\" maxlength=\"5\" />"
                             + "       </div>"
                             + "   </div><hr class=\"simple\" color=\"#d2d6de\" />	";
            return div;
        }

        function addValidate() {
            $('#form1').bootstrapValidator('addField', 'txtFeeName', {
                validators: {
                    notEmpty: {
                        message: '自定义费用名称不能为空'
                    }
                }
            });
            $('#form1').bootstrapValidator('addField', 'txtFee', {
                validators: {
                    notEmpty: {
                        message: '工本费不能为空'
                    },
                    regexp: {
                        regexp: /^[0-9]\d*(\.\d{0,2})?$/,
                        message: '请输入正确的金额类型，整数或小数，其中小数点后最多包含2位数字'
                    }
                }
            });
        }

        function deleteRow(obj)
        {
            obj.parent().parent().next().remove();
            obj.parent().parent().next().remove();
            obj.parent().parent().remove();

        }

        function initFormValidate() {
            $("#form1").bootstrapValidator({
                submitHandler: function (valiadtor, loginForm, submitButton) {
                    valiadtor.defaultSubmit();
                },
                fields: {
                    txtFee: {
                        validators: {
                            notEmpty: {
                                message: '工本费不能为空'
                            },
                            regexp: {
                                regexp: /^[0-9]\d*(\.\d{0,2})?$/,
                                message: '请输入正确的金额类型，整数或小数，其中小数点后最多包含2位数字'
                            }
                        }
                    }
                }
            });
        }

        //保存
        function save()
        {
            var bootstrapValidator = $("#form1").data('bootstrapValidator');
            bootstrapValidator.validate();

            if (bootstrapValidator.isValid()) {
                var $namelist = "工本费";
                var $vallist = $("#txtFee").val();
                var $typelist = "1";
                var $idlist = $("#priceIds").val();
                var $selectList = $("#CustomBody");
                $selectList.find(".form-group").each(function () {
                    var $this = $(this);
                    if ($this.find("input[name='txtFeeName']").val() != null) {
                        $namelist += "," + $this.find("input[name='txtFeeName']").val();
                    }
                    if ($this.find("input[name='txtFee']").val() != null) {
                        $vallist += "," + $this.find("input[name='txtFee']").val();
                        $typelist +=",-1";
                    }
                    if ($this.find("input[name='hidFeeId']").val() != null&&$this.find("input[name='hidFeeId']").val() != "") {
                        $idlist += "," + $this.find("input[name='hidFeeId']").val();
                    }
                });

                BMCW.ajax({
                    url: '@Url.Content("~/Config/SaveConfigPrice")',
                    type: "Post",
                    data: {
                        CityID:@ViewBag.CityId,
                        BizTypeId: @ViewBag.BizTypeId,
                        FeeNameArr: $namelist,
                        FeeArr: $vallist,
                        FeeTypeArr: $typelist,
                        FeeConfigIds:$idlist
                    },
                    success: function (o) {
                        if (o.status == 0&&o.data==true) {
                            BMCW.msg("保存成功",function(){
                                $("#CustomBody").html("");
                                initFee();
                            });
                        } else {
                            BMCW.alert("保存失败");
                        }
                    }
                })
            }
        }

        //取消，跳回上一页面
        function cancel()
        {
            history.go(-1);
        }

        //初始化数据
        function initFee()
        {
            BMCW.ajax({
                url: '@Url.Content("~/Config/GetNetWorkFeeConfigList")?cityId=@ViewBag.CityId&bizType=@ViewBag.BizTypeId',
                type: "Get",
                success: function (o) {
                    if (o.status == 0) {
                         $.each(o.data,function(i){
                            var obj = o.data[i];
                            if(i==0){
                                $("#txtFee").val(obj.Fee);
                                $("#priceIds").val(obj.FeeConfigID);
                            }else
                            {
                                var row = divFeeRow(obj.FeeName,obj.Fee,obj.FeeConfigID);
                                $("#CustomBody").append(row);
                                addValidate();
                            }                            
                        });
                    } 
                }
            });

        }
    </script>
}